package com.huidao.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.huidao.entity.Role;
import com.huidao.util.JDBCUtil;

public class RoleDaoImpl implements RoleDao {
	
	Connection conn = null;
	PreparedStatement ps =null;
	ResultSet rs =null;
	
	@Override
	public boolean addRole(Role role) {
		String sql = "INSERT INTO ROLE(ROLE_NAME) VALUES(?)";
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setString(1, role.getRoleName());
			int i  =ps.executeUpdate();
			if(i>0){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return false;
	}

	@Override
	public boolean delRole(Long id) {
		
		String sql = "DELETE FROM ROLE WHERE ROLE_ID=?";
		
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setLong(1, id);
			int i  =ps.executeUpdate();
			if(i>0){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return false;
	}

	@Override
	public boolean updateRole(Role role) {
		String sql = "UPDATE ROLE SET ROLE_NAME=? " +
				"WHERE ROLE_ID=?";
		
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setString(1, role.getRoleName());
			ps.setLong(2, role.getRoleId());
			int i  =ps.executeUpdate();
			if(i>0){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return false;
	}

	@Override
	public List<Role> getListPage(int currentPage, int pageSize, String namekey) {
		List<Role> list = new ArrayList<Role>();
		String sql = "SELECT * FROM ROLE  WHERE 1=1 ";
		if(namekey!=null&&!namekey.trim().equals("")){
			sql = sql +" AND ROLE_NAME LIKE '%"+namekey+"%' ";
		}
			sql = sql +	" LIMIT ?,?";
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, (currentPage-1)*pageSize);
			ps.setInt(2, pageSize);
			rs =ps.executeQuery();
			while(rs.next()){
				Role b = new Role();
				b.setRoleId(rs.getLong("ROLE_ID"));
				b.setRoleName(rs.getString("ROLE_NAME"));
				list.add(b);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return list;
	}

	@Override
	public int getTotalCount(String namekey) {
		String sql = "SELECT COUNT(*) FROM ROLE WHERE 1=1 ";
		if(namekey!=null&&!namekey.trim().equals("")){
			sql = sql +" AND ROLE_NAME LIKE '%"+namekey+"%' ";
		}
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			rs =ps.executeQuery();
			if(rs.next()){
				return rs.getInt(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return 0;
	}

	@Override
	public List<Role> getAllList() {
		List<Role> list = new ArrayList<Role>();
		String sql = "SELECT * FROM ROLE ";
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			rs =ps.executeQuery();
			while(rs.next()){
				Role b = new Role();
				b.setRoleId(rs.getLong("ROLE_ID"));
				b.setRoleName(rs.getString("ROLE_NAME"));
				list.add(b);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return list;
	}

}
